package com.sanpu.kxe.business.main.view;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.adapter.PromotePosterAdapter;
import com.sanpu.kxe.business.main.bean.PromotePosterOutput;
import com.sanpu.kxe.business.main.contract.PromotePosterContract;
import com.sanpu.kxe.business.main.presenter.PromotePosterPresenter;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.module.widget.VerticalSwipeRefreshLayout;
import com.sanpu.kxe.utils.FileUtil;

import java.io.File;
import java.util.List;

import butterknife.Bind;

/**
 * Description：推广海报
 * Created by 薛勇军 on 2018/8/20.
 */

public class PromotePosterActivity extends BaseTitleActivity implements PromotePosterContract.View {

    @Bind(R.id.list_view)
    RecyclerView vRecyclerview;
    @Bind(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @Bind(R.id.emptyView)
    View emptyView;

    private PromotePosterAdapter mAdapter;

    private PromotePosterContract.Presenter mPresenter;

    private String filePath;

    private int nowPage = 0;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_promote_poster;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("推广海报");
        mPresenter = new PromotePosterPresenter(this);
        vRecyclerview.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mAdapter = new PromotePosterAdapter(mPresenter.getPromoteClickListener());
        vRecyclerview.setAdapter(mAdapter);
        mAdapter.disableLoadMoreIfNotFullPage(vRecyclerview);
        mAdapter.setPreLoadNumber(2);
        mPresenter.getPosterList(nowPage);
    }

    @Override
    protected void initData() {
        super.initData();
        filePath = Environment.getExternalStorageDirectory().getAbsolutePath();
        filePath += File.separator + "qlch" + File.separator + "金控宣传"+System.currentTimeMillis();
        mPresenter.setFilePath(filePath);
    }

    @Override
    protected void setListener() {
        super.setListener();
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = 0;
                mPresenter.getPosterList(nowPage);
                mAdapter.setEnableLoadMore(true);
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getPosterList(nowPage);
            }
        }, vRecyclerview);
    }

    @Override
    public void showPromoteListView(List<PromotePosterOutput> posterOutputs, boolean isFirst) {
        if (posterOutputs != null && posterOutputs.size()>0) {
            vSwipeRefreshLayout.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
            if (isFirst) {
                mAdapter.setNewData(posterOutputs);
            } else {
                mAdapter.addData(posterOutputs);
            }
            nowPage++;
        } else if (isFirst){
            vSwipeRefreshLayout.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                mAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                mAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                mAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    public void setNotify(int position, String downloadNum) {
        mAdapter.setNotifyData(position, downloadNum);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CommonConfig.REQUESTCODE_PROMOTE_POSTER) {
            FileUtil.deleteImgFiles(filePath);
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(PromotePosterContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
